﻿
namespace EasyNet.Solr.Impl
{
    using System.Collections.Generic;

    using Commons.Params;

    /// <summary>
    /// Solr查询奥佐
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SolrQueryOperations<T> : ISolrQueryOperations<T>
    {
        private readonly ISolrReadonlyConnection<T> connection;
        private readonly string handler = "/select";

        /// <summary>
        /// Solr的wt参数
        /// </summary>
        public string ResponseWriter { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connection">Solr链接</param>
        public SolrQueryOperations(ISolrReadonlyConnection<T> connection)
        {
            this.connection = connection;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="handler">Solr查询handler</param>
        /// <param name="connection">Solr连接</param>
        public SolrQueryOperations( string handler,ISolrReadonlyConnection<T> connection)
            : this(connection)
        {
            this.handler = handler;
        }

        #region ISolrQueryOperations Members

        public T Query(ISolrQuery query, IDictionary<string, ICollection<string>> options)
        {
            options = options ?? new Dictionary<string, ICollection<string>>();

            if (!string.IsNullOrEmpty(ResponseWriter) && !options.ContainsKey(CommonParams.WT))
            {
                options[CommonParams.WT] = new string[] { ResponseWriter };
            }

            if (query != null && !string.IsNullOrEmpty(query.Query))
            {
                options[CommonParams.Q] = new string[] { query.Query };
            }

            return connection.Get(handler, options);
        }

        #endregion
    }
}
